<template>
    <el-container direction="vertical">
        <el-header>
            <el-row :gutter="24">
                <el-col :span="2" :offset="0">
                    <div class="log header">
                        <h1>ERP系统</h1>
                    </div>
                </el-col>
                <el-col :span="2" :offset="0">
                    <div class="header">
                        <span class="login_name">{{ user.loginName }}</span>
                    </div>
                </el-col>
                <el-col :span="7" :offset="13">
                    <div class="user-wrapper header">
                        <span class="action">
                            <i>
                                <svg viewBox="64 64 896 896" data-icon="search" width="1.2em" height="1.2em"
                                    fill="currentColor" aria-hidden="true" focusable="false" class="">
                                    <path
                                        d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0 0 11.6 0l43.6-43.5a8.2 8.2 0 0 0 0-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z">
                                    </path>
                                </svg>
                            </i>
                        </span>
                        <span class="borders" title="搜索菜单">
                            <el-select class="borders-select" placeholder="搜索菜单" clearable filterable remote
                                @change="changeEvent">
                                <el-option v-for="item in sys.getLayoutValue" :key="item.id" :label="item.cname"
                                    :value="item">
                                </el-option>
                            </el-select>

                        </span>
                        <span class="action user-up">
                            <el-dropdown placement="bottom" @command="handleCommand">
                                <el-button style="font-size: 16px;">欢迎您，{{ user.username }}</el-button>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item>修改密码</el-dropdown-item>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </span>
                        <span class="action">
                            <el-popconfirm title="真的要注销登录吗？">
                                <template #reference>
                                    <a href="javascript:void(0)" class="logout_title">
                                        <el-icon>
                                            <SwitchButton />
                                        </el-icon>
                                        <span>&nbsp;退出登录</span>
                                    </a>
                                </template>
                                <template #actions="{ confirm, cancel }">
                                    <el-button size="small" @click="cancel">取消</el-button>
                                    <el-button type="danger" size="small" @click="quit">
                                        确定
                                    </el-button>
                                </template>
                            </el-popconfirm>
                        </span>
                    </div>
                </el-col>
            </el-row>
        </el-header>
        <el-container direction="horizontal">
            <el-aside width="200px">
                <el-menu mode="vertical" class="el-menu-vertical-demo" :collapse="isCollapse">
                    <el-menu-item @click="toView">
                        <span>
                            <el-icon>
                                <House />
                            </el-icon>
                        </span>
                        <span>
                            首页
                        </span>
                    </el-menu-item>
                    <SubItem :data="sys.treeMenus" @function="getBtnStr"></SubItem>
                </el-menu>
            </el-aside>
            <el-container direction="vertical">
                <el-tabs v-model="sys.activeTab" tab-position="top" @tab-remove="handlerClickRemove"
                    @tab-click="handleClick">
                    <el-tab-pane :closable="false" label="首页" name="home">
                    </el-tab-pane>
                    <template v-for="item in sys.tabs">
                        <el-tab-pane :closable="true" :label="item.cname" :name="item.ename"></el-tab-pane>
                    </template>
                </el-tabs>
                <el-main>
                    <RouterView v-slot="{ Component }" :strBtn="strBtn">
                        <transition name="fade" mode="out-in">
                            <component :is="Component" />
                        </transition>
                    </RouterView>
                </el-main>
            </el-container>
        </el-container>
    </el-container>



    <el-dialog :title="dialogTitle" v-model="visible" width="36%" max-height="80vh" draggable>
        <el-form :model="upPwd" ref="form" :rules="rules" label-width="auto" :inline="false" status-icon>
            <el-form-item label="旧密码：" prop="oldPwd" required>
                <el-input v-model="upPwd.oldPwd"></el-input>
            </el-form-item>
            <el-form-item label="新密码：" prop="newPwd" required>
                <el-input v-model="upPwd.newPwd"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码：" prop="newPwdTwo" required>
                <el-input v-model="upPwd.newPwdTwo"></el-input>
            </el-form-item>
            <div class="button-container">
                <el-form-item class="up-btn" style="display: flex; justify-content: center; align-items: center;">
                    <el-button type="primary" @click="updatePwd(form)">确定</el-button>
                    <el-button @click="visible = false">取消</el-button>
                </el-form-item>
            </div>
        </el-form>
    </el-dialog>
</template>

<style scoped>
.fade-enter-from {
    /* 进入时的透明度为0 和 刚开始进入时的原始位置通过active透明度渐渐变为1 */
    opacity: 0;
    transform: translateX(30%);
}

.fade-enter-to {
    /*定义进入完成后的位置 和 透明度 */
    transform: translateX(0%);
    opacity: 1;
}

.fade-leave-active,
.fade-enter-active {
    transition: all 0.5s ease-out;
}

.fade-leave-from {
    /* 页面离开时一开始的css样式,离开后为leave-to，经过active渐渐透明 */
    transform: translateX(0%);
    opacity: 1;
}

.fade-leave-to {
    /* 这个是离开后的透明度通过下面的active阶段渐渐变为0 */
    transform: translateX(-30%);
    opacity: 0;
}
</style>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import SubItem from '@/views/SubItem.vue';
import useSys from '@/store/useSys';
import router from '@/router';
import request from '@/api/request'

let sys = useSys()
let form = ref(null)
let isCollapse = ref(false)
let dialogTitle = ref('') // 弹窗标题
let visible = ref(false) // 退出登录弹窗
let upPwd = ref({// 修改密码参数
    oldPwd: '',
    newPwd: '',
    newPwdTwo: ''
})

// 登录用户
let user = sys.sessionUser;
// 表单效验规则
let rules = reactive({
    oldPwd: [
        { required: true, message: '请输入旧密码', trigger: 'blur' }
    ],
    newPwd: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        { min: 6, max: 18, message: '密码长度为6~18位', trigger: 'blur' },
        { pattern: /^[a-zA-Z0-9_]{6,18}$/, message: '密码由字母、数字、下划线组成', trigger: 'blur' }
    ],
    newPwdTwo: [
        { required: true, message: '请确认新密码', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (value !== upPwd.value.newPwd) {
                    callback(new Error('两次输入的密码不一致'));
                } else {
                    callback()
                }
            }, trigger: 'blur'
        }
    ]
});

let handleClick = (tab) => {
    router.push({ name: tab.paneName });
}
let handlerClickRemove = (tab) => {
    for (let i = 0; i < sys.tabs.length; i++) {
        if (sys.tabs[i].ename == tab) {
            //如果是选中的选项卡那么就需要动态的切换到上一个选项卡
            if (tab == sys.activeTab) {
                if (i == 0) {
                    sys.activeTab = "home"
                } else {
                    sys.activeTab = sys.tabs[i - 1].ename;
                }
                router.push({ name: sys.activeTab })
            }
            sys.tabs.splice(i, 1);
            return;
        }
    }
}
let toView = () => {
    router.push("/")
    sys.activeTab = "home"
}

let strBtn = ref({})
const getBtnStr = async (item: any) => {
    await request.get('roleMenu/getBtnStr', {
        params: {
            menuId: item.id
        }
    }).then(resp => {
        strBtn.value = resp.data

    })
}

// 点击搜索菜单切换路由和标签页
let changeEvent = (item) => {
    router.push(item.path)
    sys.handleClickMenu(item);
}

// 修改密码操作
const handleCommand = () => {
    dialogTitle.value = '修改密码'
    visible.value = true;
}

// 退出登录操作
let quit = () => {
    sys.quit().then(resp => {
        if (resp) {
            router.push("/login")
        }
    })
}

// 修改密码
let updatePwd = (formEl) => {
    console.log("验证密码")
    formEl.validate((v: any) => {
        if (v) {
            visible.value = false;
        }
    });
}
</script>



<style scoped>
.el-menu {
    position: relative;
    height: 100%;
}

.el-container {
    height: 100%;
}

.el-header {
    color: white;
    background-color: #1890ff;
}

.el-col .log,
.el-col .header {
    display: flex;
    height: 60px;
}

.log h1,
.header .login_name {
    margin: 0px;
    padding: 0px;
    align-self: center;
}

.user-wrapper {
    align-items: center;
}

.user-wrapper .action:hover {
    background-color: #5eb2ff;
}

.user-wrapper .action {
    cursor: pointer;
    padding: 0 14px;
    display: inline-block;
    transition: all .3s;
    height: 70%;
    line-height: 50px;
}

.user-wrapper .borders {
    width: 100px;
}

.el-select__wrapper.is-filterable {
    background-color: transparent;
}

.action .el-dropdown {
    align-items: center;
}

.action .el-dropdown .el-button {
    background-color: transparent;
    color: white;
    border: 0px solid;
    height: 42px;
}

.logout_title {
    display: flex;
    color: inherit;
    text-decoration: none;
    align-items: center;
    height: 42px;
}

.logout_title span {
    vertical-align: middle
}

:global(.button-container) {
    margin-top: 26px;
    padding-top: 22px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: 1px solid #e8e8e8;
}

:deep(.el-tooltip__trigger:focus-visible) {
    outline: unset;
}

.el-main {
    /* background-color: white; */
    /* position: relative; */
    margin: 4px 4px 0;
}

/* 设置滚动条样式 */
.el-aside::-webkit-scrollbar {
    display: none;
}

:deep(.el-tabs--top>.el-tabs__header .el-tabs__item:nth-child(2)) {
    padding-left: 20px;
}

:deep(.el-tabs>.el-tabs__header) {
    margin-bottom: 5px;
}

:deep(.el-overlay>.dialog) {
    max-height: 80vh;
}

.el-dialog {
    max-height: 80vh;
}
</style>
